package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/*
聊天室服务端
 */
public class Server {
    private ServerSocket serverSocket;
    private List<PrintWriter> list = new ArrayList<>();
    public Server() {

        try {
            //创建端口号
            System.out.println("服务端创建...");
            serverSocket = new ServerSocket(8088);
            System.out.println("客服端链接创建成功。");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        while (true) {
            try {
                System.out.println("等待客户端连接...");
                Socket socket = serverSocket.accept();//截断等待链接
                System.out.println("一个用户端已链接！");
                //创建流
                //启动线程
                ClientHandler ch = new ClientHandler(socket);
                Thread t = new Thread(ch);
                t.start();
            } catch (IOException e) {
                System.out.println("xxx意外断开链接。");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;//记录客户端信息
        public ClientHandler(Socket socket) {
            this.socket = socket;
            host =socket.getInetAddress().getHostAddress();
        }
        @Override
        public void run() {
            PrintWriter pw=null;
            try {
                //低级流(输入)
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                //低级流(输出)
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw =new OutputStreamWriter(os, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);
                //将输入流存入共享集合list
                synchronized (list){
                    list.add(pw);
                }

                System.out.println(host+"上线了，当前上线人数："+list.size());

                //转换流是？？？
                String message ;
                while((message = br.readLine())!=null){
                    System.out.println(host+"用户："+message);
                    synchronized (list){
                        for(PrintWriter pw1 :list){//迭代器遍历不能修改集合；
                            if(pw != pw1){//不发消息给自己
                                pw1.println(host+"说："+message);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                System.out.println("xxx意外断开链接。");
                e.printStackTrace();
            }finally {
                //处理客户端断开操作
                synchronized(list){
                    list.remove(pw);
                }
                System.out.println(host + "下线了，当前在线人数："+list.size());
                try {
                    socket.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
